package com.tedu.ds.dict.service.impl;

import com.tedu.ds.dict.mapper.DictMapper;
import com.tedu.ds.dict.pojo.dto.DictParam;
import com.tedu.ds.dict.pojo.dto.DictQuery;
import com.tedu.ds.dict.pojo.entity.Dict;
import com.tedu.ds.dict.pojo.vo.DictVO;
import com.tedu.ds.dict.service.DictService;
import com.tedu.ds.dictOption.mapper.DictOptionMapper;
import com.tedu.ds.dictOption.pojo.dto.DictOptionParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Override
    public List<DictVO> select(DictQuery dictQuery) {
        return dictMapper.select(dictQuery);
    }

    @Override
    public void save(DictParam dictParam) {
        Dict dict=new Dict();
        BeanUtils.copyProperties(dictParam,dict);
        if (dict.getId()==null){
            dictMapper.insert(dict);
        }else {
            dictMapper.update(dict);
        }
    }

    @Autowired
    private DictOptionMapper dictOptionMapper;

    @Override
    public void deleteById(Integer id) {
        dictMapper.deleteById(id);
        DictOptionParam dictOptionParam=new DictOptionParam();
        dictOptionParam.setDictId(id);
        dictOptionMapper.delete(dictOptionParam);
    }
}
